﻿#include "SaveTerminalToModelDlg.h"
#include "ui_SaveTerminalToModelDlg.h"
#include <QMessageBox>
#include <QDateTime>


SaveTerminalToModelDlg::SaveTerminalToModelDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SaveTerminalToModelDlg)
{
	m_YxNum = 0;
	m_YcNum = 0;
	m_YkNum = 0;
	m_YmNum = 0;
	m_SetNum = 0;
	m_columnNum = 0;
	m_RecordLength = 0;
	m_pColumnInfo = NULL;
	m_pDataOffset = NULL;
    ui->setupUi(this);

	connect(ui->pAffirmBtn, SIGNAL(clicked()), this, SLOT(AffirmBtn()));
	connect(ui->pCancelBtn, SIGNAL(clicked()), this, SLOT(close()));

}

SaveTerminalToModelDlg::~SaveTerminalToModelDlg()
{
    delete ui;
}

void SaveTerminalToModelDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//确认
void SaveTerminalToModelDlg::AffirmBtn()
{
	QString ModelName;
	ModelName = ui->pModelNameEdit->text();
	if (ModelName.length()<=0)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("未输入设备模板名称！"), QMessageBox::Ok | QMessageBox::Cancel);
		return ;
	}
	if (ModelName.length() > STRING_NORMAL_LEN)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("设备模板名称过长！"), QMessageBox::Ok | QMessageBox::Cancel);
		return ;
	}
	setCursor(QCursor(Qt::WaitCursor));

	InsertDevModel(ModelName);
	
	setCursor(QCursor(Qt::ArrowCursor));
	return accept();
}

void SaveTerminalToModelDlg::ReleasColumnInfo()
{
	if (m_pColumnInfo)
	{
		delete [](char*)m_pColumnInfo;
		m_pColumnInfo = NULL;
	}
	if (m_pDataOffset)
	{
		delete [](int*)m_pDataOffset;
		m_pDataOffset = NULL;
	}
	if (m_columnNum>0)
	{
		m_columnNum = 0;
	}
	if (m_RecordLength>0)
	{
		m_RecordLength = 0;
	}
}

void SaveTerminalToModelDlg::InsertDevModel(QString ModeName)
{
	if (ModeName.length()<=0 || ModeName.length() > STRING_NORMAL_LEN)
	{
		return ;
	}
	
	rDevModel_Info		rDevMode;
	unsigned int		RecordId = 0;
	int		ModelYxNum = 0;
	int		ModelYcNum = 0;
	int		ModelYkNum = 0;
	int		ModelYmNum = 0;
	int		ModelRybNum = 0;
	int		ModelBhYcNum = 0;
	int		ModelSetNum = 0;
	int		ModelParamNum = 0;
	int		ModelPrecNum = 0;
	int		ModelAreaNum = 0;
	g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, RecordId);
	if (RecordId<=0)
	{
		return ;
	}
	int	SetNum=0;
	int	ParaNum=0;
	int	BhycNum=0;
	int	PrecNum=0;
	int	RybNum=0;
	int	AreaNum=0;
	
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, &m_YxNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC, &m_YcNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK, &m_YkNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM, &m_YmNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET, &SetNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_DEV, &ParaNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC, &BhycNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB, &RybNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA, &AreaNum);

	rDevMode.ID = RecordId;
	sprintf(rDevMode.Name, "%s", ModeName.toLocal8Bit().data());

	rDevMode.YxNum = m_YxNum;
	rDevMode.YcNum = m_YcNum;
	rDevMode.YkNum = m_YkNum;
	rDevMode.YmNum = m_YmNum;
	rDevMode.SetNum = SetNum;
	rDevMode.ParaNum = ParaNum;
	rDevMode.BhycNum = BhycNum;
	rDevMode.PrecNum = PrecNum;
	rDevMode.RybNum = RybNum;
	rDevMode.AreaNum = AreaNum;

	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return ;
	}

	Ret = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, (char*)&rDevMode, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
	if (Ret<=0)
	{
		ReleasColumnInfo();
		return;
	}
	unsigned int lastTime;
	unsigned int CurTime;
	CurTime = GetTickCount();
	lastTime = CurTime;

	ModelYxNum = InsertYxModel(rDevMode.ID);
	ModelYcNum = InsertYcModel(rDevMode.ID);
	ModelYkNum = InsertYkModel(rDevMode.ID);
	ModelYmNum = InsertYmModel(rDevMode.ID);
	ModelBhYcNum = InsertBhYcModel(rDevMode.ID);
	ModelSetNum = InsertSetModel(rDevMode.ID);
	ModelParamNum = InsertParamModel(rDevMode.ID);
	ModelPrecNum = InsertPrecModel(rDevMode.ID);
	ModelRybNum = InsertRybModel(rDevMode.ID);
	ModelAreaNum = InsertSetAreaModel(rDevMode.ID);

	CurTime = GetTickCount();
//	qDebug()<<(CurTime-lastTime);

	QString Str;
	Str = QString::fromLocal8Bit("保存设备模板“%1”: \
\r\n YxNum:%2\r\n YcNum:%3\r\n YkNum:%4\r\n YmNum:%5 \r\n \
BhYcNum:%6\r\n SetNum:%7\r\n ParamNum:%8\r\n PrecNum:%9 \r\n \
RybNum:%10\r\n AreaNum:%11 %12\r\n")
		.arg(QString::fromLocal8Bit(rDevMode.Name))
		.arg(ModelYxNum)
		.arg(ModelYcNum)
		.arg(ModelYkNum)
		.arg(ModelYmNum)
		.arg(ModelBhYcNum)
		.arg(ModelSetNum)
		.arg(ModelParamNum)
		.arg(ModelPrecNum)
		.arg(ModelRybNum)
		.arg(ModelAreaNum)
		.arg((CurTime-lastTime));
	QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), Str, QMessageBox::Ok | QMessageBox::Cancel);
	ReleasColumnInfo();
	emit UpDateTreeItem_Signal(rDevMode.ID);
	return ;
}

int SaveTerminalToModelDlg::InsertYxModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int	buff_len = 0;
	int	YxNum = 0;
	int	i = 0;
	unsigned int RecordId = 0;
	rDevModel_Yx DevModeYx;
	QString		 NameCh;
	int		Retval = 0;
	int		DevModexYxNum = 0;
	ReleasColumnInfo();

	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YxNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, buff, &buff_len, true);
	if (YxNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<YxNum; i++)
		{
			DevModeYx.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYx.IndexNo = DevModexYxNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_Yx*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYx.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYx.Fun = ((rParamS_Yx*)buff)[i].m_fun;
			DevModeYx.Inf = ((rParamS_Yx*)buff)[i].m_inf;
			DevModeYx.YxType = ((rParamS_Yx*)buff)[i].m_yx_type;
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYx.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, (char*)&DevModeYx, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYxNum++;
		}

		g_pSqLiteDB.CommitTransaction();
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYxNum;
}

int SaveTerminalToModelDlg::InsertYcModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int	buff_len = 0;
	int	YcNum = 0;
	int	i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Yc DevModeYc;
	QString		 NameCh;
	int		Retval = 0;
	int		DevModexYcNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YcNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC, buff, &buff_len, true);
	if (YcNum>0 && buff)
	{

	g_pSqLiteDB.BeginTransaction();

		for (i=0; i<YcNum; i++)
		{
			DevModeYc.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYc.IndexNo = DevModexYcNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_Yc*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYc.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYc.Fun = ((rParamS_Yc*)buff)[i].m_fun;
			DevModeYc.Inf = ((rParamS_Yc*)buff)[i].m_inf;
			DevModeYc.percent = ((rParamS_Yc*)buff)[i].percent;
			DevModeYc.rating = ((rParamS_Yc*)buff)[i].rating;
			DevModeYc.ratio = ((rParamS_Yc*)buff)[i].ratio;
			DevModeYc.offset = ((rParamS_Yc*)buff)[i].offset;
			DevModeYc.High_limit = ((rParamS_Yc*)buff)[i].High_limit;
			DevModeYc.Low_limit = ((rParamS_Yc*)buff)[i].Low_limit;
			DevModeYc.tYCLimit = ((rParamS_Yc*)buff)[i].tYCLimit;
			memcpy(DevModeYc.unit, ((rParamS_Yc*)buff)[i].unit, UNIT_NAME_LEN);
			
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYc.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, (char*)&DevModeYc, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYcNum++;
		}

	g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYcNum;
}

int SaveTerminalToModelDlg::InsertYkModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char		 *buff = NULL;
	int		 buff_len = 0;
	int		 YkNum = 0;
	int		 i = 0;
	unsigned int		 RecordId = 0;
	rDevModel_Yk DevModeYk;
	QString		 NameCh;
	int		 Retval = 0;
	int		 DevModexYkNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YkNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK, buff, &buff_len, true);
	if (YkNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<YkNum; i++)
		{
			DevModeYk.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYk.IndexNo = DevModexYkNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_Yk*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYk.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYk.Fun = ((rParamS_Yk*)buff)[i].m_fun;
			DevModeYk.Inf = ((rParamS_Yk*)buff)[i].m_inf;
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYk.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, (char*)&DevModeYk, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYkNum++;
		}

		g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYkNum;
}

int SaveTerminalToModelDlg::InsertYmModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char		 *buff = NULL;
	int		 buff_len = 0;
	int		 YmNum = 0;
	int		 i = 0;
	unsigned int		 RecordId = 0;
	rDevModel_Ym DevModeYm;
	QString		 NameCh;
	int		 Retval = 0;
	int		 DevModexYmNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YmNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM, buff, &buff_len, true);
	if (YmNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<YmNum; i++)
		{
			DevModeYm.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYm.IndexNo = DevModexYmNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_Ym*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYm.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYm.Fun = ((rParamS_Ym*)buff)[i].m_fun;
			DevModeYm.Inf = ((rParamS_Ym*)buff)[i].m_inf;
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYm.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, (char*)&DevModeYm, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYmNum++;
		}

		g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYmNum;
}

int SaveTerminalToModelDlg::InsertBhYcModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int	buff_len = 0;
	int	BhYcNum = 0;
	int	i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Bhyc ModelBhYc;
	QString		 NameCh;
	int		Retval = 0;
	int		DevModexBhYcNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	BhYcNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC, buff, &buff_len, true);
	if (BhYcNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<BhYcNum; i++)
		{
			ModelBhYc.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			ModelBhYc.IndexNo = DevModexBhYcNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_BhYc*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(ModelBhYc.Name, "%s", NameCh.toLocal8Bit().data());
			ModelBhYc.Fun = ((rParamS_BhYc*)buff)[i].m_fun;
			ModelBhYc.Inf = ((rParamS_BhYc*)buff)[i].m_inf;
			ModelBhYc.percent = ((rParamS_BhYc*)buff)[i].percent;
			ModelBhYc.rating = ((rParamS_BhYc*)buff)[i].rating;
			ModelBhYc.ratio = ((rParamS_BhYc*)buff)[i].ratio;
			ModelBhYc.offset = ((rParamS_BhYc*)buff)[i].offset;
			ModelBhYc.High_limit = ((rParamS_BhYc*)buff)[i].High_limit;
			ModelBhYc.Low_limit = ((rParamS_BhYc*)buff)[i].Low_limit;
			ModelBhYc.tYCLimit = ((rParamS_BhYc*)buff)[i].tYCLimit;
			memcpy(ModelBhYc.unit, ((rParamS_BhYc*)buff)[i].unit, UNIT_NAME_LEN);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			ModelBhYc.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, (char*)&ModelBhYc, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexBhYcNum++;
		}

		g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexBhYcNum;
}


int SaveTerminalToModelDlg::InsertSetModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int	buff_len = 0;
	int	SetNum = 0;
	int	i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Set   ModelSet;
	QString			NameCh;
	int				Retval = 0;
	int				DevModexSetNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	SetNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET, buff, &buff_len, true);
	if (SetNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<SetNum; i++)
		{
			ModelSet.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			ModelSet.IndexNo = DevModexSetNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_Set*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(ModelSet.Name, "%s", NameCh.toLocal8Bit().data());
			ModelSet.Fun = ((rParamS_Set*)buff)[i].m_fun;
			ModelSet.Inf = ((rParamS_Set*)buff)[i].m_inf;
			ModelSet.daType = ((rParamS_Set*)buff)[i].daType;
			ModelSet.daFormat = ((rParamS_Set*)buff)[i].daFormat;
			ModelSet.defval = ((rParamS_Set*)buff)[i].defval;
			ModelSet.min_val = ((rParamS_Set*)buff)[i].min_val;
			ModelSet.max_val = ((rParamS_Set*)buff)[i].max_val;
			ModelSet.step_val = ((rParamS_Set*)buff)[i].step_val;
			memcpy(ModelSet.unit, ((rParamS_Set*)buff)[i].unit, UNIT_NAME_LEN);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			ModelSet.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET, (char*)&ModelSet, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexSetNum++;
		}

		g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexSetNum;
}


int SaveTerminalToModelDlg::InsertParamModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int		buff_len = 0;
	int		ParaNum = 0;
	int		i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Para   ModelParam;
	QString			NameCh;
	int				Retval = 0;
	int				DevModexParaNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	ParaNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_DEV, buff, &buff_len, true);
	if (ParaNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<ParaNum; i++)
		{
			ModelParam.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			ModelParam.IndexNo = DevModexParaNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_Dev*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(ModelParam.Name, "%s", NameCh.toLocal8Bit().data());
			ModelParam.Fun = ((rParamS_Dev*)buff)[i].m_fun;
			ModelParam.Inf = ((rParamS_Dev*)buff)[i].m_inf;
			ModelParam.daType = ((rParamS_Dev*)buff)[i].daType;
			ModelParam.daFormat = ((rParamS_Dev*)buff)[i].daFormat;
			ModelParam.defval = ((rParamS_Dev*)buff)[i].defval;
			ModelParam.min_val = ((rParamS_Dev*)buff)[i].min_val;
			ModelParam.max_val = ((rParamS_Dev*)buff)[i].max_val;
			ModelParam.step_val = ((rParamS_Dev*)buff)[i].step_val;
			memcpy(ModelParam.unit, ((rParamS_Dev*)buff)[i].unit, UNIT_NAME_LEN);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			ModelParam.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, (char*)&ModelParam, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexParaNum++;
		}

		g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexParaNum;
}


int SaveTerminalToModelDlg::InsertPrecModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int		buff_len = 0;
	int		PrecNum = 0;
	int		i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Prec   ModelPrec;
	QString			NameCh;
	int				Retval = 0;
	int				DevModexPrecNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	PrecNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_JDXS, buff, &buff_len, true);
	if (PrecNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<PrecNum; i++)
		{
			ModelPrec.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			ModelPrec.IndexNo = DevModexPrecNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_JDXS*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(ModelPrec.Name, "%s", NameCh.toLocal8Bit().data());
			ModelPrec.defval = ((rParamS_JDXS*)buff)[i].defval;
			ModelPrec.min_val = ((rParamS_JDXS*)buff)[i].min_val;
			ModelPrec.max_val = ((rParamS_JDXS*)buff)[i].max_val;

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			ModelPrec.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, (char*)&ModelPrec, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexPrecNum++;
		}

		g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexPrecNum;
}


int SaveTerminalToModelDlg::InsertRybModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int		buff_len = 0;
	int		RybNum = 0;
	int		i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Ryb   ModelRyb;
	QString			NameCh;
	int				Retval = 0;
	int				DevModexRybNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	RybNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB, buff, &buff_len, true);
	if (RybNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<RybNum; i++)
		{
			ModelRyb.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			ModelRyb.IndexNo = DevModexRybNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_Ryb*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(ModelRyb.Name, "%s", NameCh.toLocal8Bit().data());
			ModelRyb.Fun = ((rParamS_Ryb*)buff)[i].m_fun;
			ModelRyb.Inf = ((rParamS_Ryb*)buff)[i].m_inf;
		
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			ModelRyb.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB, (char*)&ModelRyb, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexRybNum++;
		}

		g_pSqLiteDB.CommitTransaction();

	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexRybNum;
}


int SaveTerminalToModelDlg::InsertSetAreaModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int		buff_len = 0;
	int		SetAreaNum = 0;
	int		i = 0;
	unsigned int		RecordId = 0;
	rDevModel_SetArea   ModelArea;
	QString				NameCh;
	int					Retval = 0;
	int					DevModexAreaNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	SetAreaNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA, buff, &buff_len, true);
	if (SetAreaNum>0 && buff)
	{

		g_pSqLiteDB.BeginTransaction();

		for (i=0; i<SetAreaNum; i++)
		{
			ModelArea.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			ModelArea.IndexNo = DevModexAreaNum+1;
			NameCh = QString::fromLocal8Bit(((rParamS_SetArea*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(ModelArea.Name, "%s", NameCh.toLocal8Bit().data());
			ModelArea.Fun = ((rParamS_SetArea*)buff)[i].m_fun;
			ModelArea.Inf = ((rParamS_SetArea*)buff)[i].m_inf;

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			ModelArea.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA, (char*)&ModelArea, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexAreaNum++;
		}

		g_pSqLiteDB.CommitTransaction();


	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexAreaNum;
}
